<?php
/**
 * 实现数据库方法
 */

class database extends AbstructDatabase {
	
	private $dbobj;
	private $connected;
	const debug = false;
	
	public function __construct(){
		$this->connected = false;
		require(getcwd().DIRECTORY_SEPARATOR."lib".DIRECTORY_SEPARATOR."class".DIRECTORY_SEPARATOR."adodb".DIRECTORY_SEPARATOR."adodb.inc.php");
		$this->dbobject = NewADOConnection("mysqlt") or die("创建数据库连接错误！");
		$this->dbobject->debug = self::debug;
	}
	
	public function getRow($sql,$condition=array()){
		if(!$this->connected)return;
		return $this->dbobject->getRow($sql.$this->constructCondition($condition));
	}
	public function getAll($sql,$condition=array()){
		if(!$this->connected)return;
		return $this->dbobject->getAll($sql.$this->constructCondition($condition));
	}
	public function Execute($sql,$condition=array()){
		if(!$this->connected)return;
		return $this->dbobject->execute($sql.$this->constructCondition($condition));
	}
	
	public function lastInsertId(){
		if(!$this->connected)return;
		return $this->dbobject->Insert_ID();
	}
	public function connect($dbhost,$user,$pass,$db,$defaultcharset){
		$this->dbobject->Connect($dbhost,$user,$pass,$db) or die("数据库连接错误！");
		$this->dbobject->execute("set NAMES ".$defaultcharset);
		$this->connected = true;
	}
	public function disconnect($handle=NULL){
		if(!$this->connected)return;
	}
	public function getPage($sql,$condition,$pagesize=1){
		if(!$this->connected)return;
	}
	
	public function getCount($table,$field="*",$condition=array()){
		if(!$this->connected)return;
		if(!is_array($condition))return;
		$sql = "select count($field) from {$table} ".$this->constructCondition($condition);
		$result = $this->dbobject->getOne($sql);
		return $result;
	}
	
	public function insert($table,$fields,&$insertid=-1){
		if(!$this->connected)return;
		if(!is_array($fields))return;
		$table = _cfg::dbPrefix.$table;
		$sql = "insert into {$table}(%s) values(%s)";
		$fieldString= "";
		$fieldValue= "";
		foreach ($fields as $field =>$value){
			if(is_numeric($fields))continue;
			$fieldString .= "`$field`,";
			$fieldValue .= "'$value',";
		}
		$fieldValue = preg_replace("/,$/",'',$fieldValue);
		$fieldString = preg_replace("/,$/",'',$fieldString);
		$execSql = sprintf($sql,$fieldString,$fieldValue);
		$this->Execute($execSql);
		$insertid = $this->lastInsertId();
	}
	
	public function delete($table,$condition=array()){
		if(!$this->connected)return;
		if(!is_array($condition))return;
		$table = _cfg::dbPrefix.$table;
		$sql = "delete from {$table} ".$this->constructCondition($condition);
		$this->Execute($sql);
	}
	public function select($table,$field,$condition=array()){
		if(!$this->connected)return;
		if(!is_array($condition))return;
		$table = _cfg::dbPrefix.$table;
		$sql = "select %s from {$table} ".$this->constructCondition($condition);
		$execSql = sprintf($sql,join(",",$field));
		$result = $this->getAll($execSql);
		return $result;
	}
	
	
	public function update($table,$fields,$condition=array()){
		if(!$this->connected)return;
		if(!is_array($fields))return;
		$table = _cfg::dbPrefix.$table;
		$sql = "update {$table} set %s ".$this->constructCondition($condition);
		$fieldString= "";
		$fieldValue= "";
		foreach ($fields as $field =>$value){
			if(is_numeric($fields))continue;
			$fieldString .= "`$field`='$value',";
		}
		$fieldString = preg_replace("/,$/",'',$fieldString);
		$execSql = sprintf($sql,$fieldString);
		$this->Execute($execSql);
	}
	
	public function testSql($sql,$execute=false){
		if(!$this->connected)return;
		return $execute?$this->Execute($sql,array()):$this->getAll($sql,array());
	}	

	/**
	 * 构造SQL条件语法
	 *
	 * @param Array $param
	 * @return  String
	 */
	private function constructCondition($param){
		if(!is_array($param))return;
		if(count($param)<1)return;
		$returnString = " where 1=1 ";
		foreach ($param as $key=>$value){
			$sp = $value[0]=='%'?'like':'=';
			$returnString .= " and $key $sp '$value'";
		}
		return $returnString;
	}
}

return md5(__FILE__);